package com.aixin.project.servlet.admin;

import com.aixin.project.data.CourseOrder;
import com.aixin.project.data.Fee;
import com.aixin.project.handler.CourseOrderHandler;
import com.aixin.project.handler.FeeHandler;
import com.aixin.project.util.Pagenation;
import com.aixin.project.util.Pagenation2;
import com.aixin.project.util.RequestUtil;
import org.apache.velocity.Template;
import org.apache.velocity.context.Context;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Sufeng
 * Date: 12-5-13
 * Time: 下午3:19
 * To change this template use File | Settings | File Templates.
 */
public class CourseOrderServlet extends AdminServlet{
    @Override
    protected Template execute(HttpServletRequest request, HttpServletResponse response, Context ctx) {
        String m = RequestUtil.getStr(request,"m","");
        if(m.equals("vf")){
            long corid = RequestUtil.getLong(request,"corid",-1);
            CourseOrder courseOrder = CourseOrderHandler.find(corid);
            if(courseOrder!=null && courseOrder.getStatus()==0){
                Fee fee = new Fee();
                fee.setType(1); //虚拟款
                fee.setOrderid(courseOrder.getId());
                fee.setUserid(courseOrder.getUserid());
                fee.setFee(courseOrder.getPrice());
                long feeid = FeeHandler.create(fee);
                if(feeid>-1){
                    ctx.put("message","虚拟打款"+fee.getFee()+"成功");
                }else{
                    ctx.put("ERROR","虚拟打款失败！");
                }
            }
        }
        ctx.put("navcor",true);
        int status = RequestUtil.getInt(request,"s",-1);
        int page = RequestUtil.getInt(request,"p",1);
        StringBuffer params = new StringBuffer();
        if(page!=1)
            params.append("&p=").append(page);
        if(status!=-1)
            params.append("&s=").append(params);
        if(params.length()>0)
            ctx.put("params",params.substring(1,params.length()));
        int psize = 10;
        List<CourseOrder> list = CourseOrderHandler.listPagenation(page,psize,status);
        int count = (int)CourseOrderHandler.count(status);
        Pagenation2 pg = new Pagenation2(page,psize,count,list,"/admin/cor",params.toString());
        ctx.put("pg",pg);
        return getTemplate("bg/courseorder.vm");
    }
}
